/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2010 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author: Victor Petrov <victor_petrov@harvard.edu>                    |
  +----------------------------------------------------------------------+
*/

/* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_vxp.h"

#include <zend_interfaces.h>
#include <zend_exceptions.h>
#include "memblock.h"

/* If you declare any globals in php_vxp.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(vxp)
*/

/* True global resources - no need for thread safety here */
static int le_vxp;
zend_class_entry* vxp_xpath_exception_ce;

/**
* XPathException
*/

static function_entry vxp_xpath_exception_functions[]=
{
	PHP_ME(XPathException, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
	{ NULL, NULL, NULL }
};

PHP_METHOD(XPathException, __construct)
{
	zval* type=NULL;
	zval* message=NULL;


    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                         "zz", &message, &type) == FAILURE) {
        RETURN_NULL();
    }

	zend_call_method(&getThis(), vxp_xpath_exception_ce->parent, &vxp_xpath_exception_ce->parent->constructor, "__construct", sizeof("__construct")-1, NULL, 2, message, type TSRMLS_CC);
}

void vxp_xpath_exception_set_location(zend_class_entry* ce, zval* exception, long line, long col TSRMLS_DC)
{
	assert(ce);
	assert(exception);

	zend_update_property_long(ce,exception,"xpath_line",sizeof("xpath_line")-1,line TSRMLS_CC); 
	zend_update_property_long(ce,exception,"xpath_column",sizeof("xpath_column")-1,col TSRMLS_CC);
}

void vxp_xpath_exception_register_constants(zend_class_entry* ce)
{
	zval* constval;

	constval=pemalloc(sizeof(*constval),1);
	INIT_PZVAL(constval);
	ZVAL_LONG(constval,VXP_ERROR_SYNTAX);

	zend_hash_add(&ce->constants_table, VXP_XPATH_EXCEPTION_SYNTAX_NAME,
				  sizeof(VXP_XPATH_EXCEPTION_SYNTAX_NAME),
				  (void*)&constval,sizeof(zval*),NULL);

	constval=pemalloc(sizeof(*constval),1);
	INIT_PZVAL(constval);
	ZVAL_LONG(constval,VXP_ERROR_MEMORY);

	zend_hash_add(&ce->constants_table, VXP_XPATH_EXCEPTION_MEMORY_NAME,
				  sizeof(VXP_XPATH_EXCEPTION_MEMORY_NAME),
				  (void*)&constval,sizeof(zval*),NULL);


	constval=pemalloc(sizeof(*constval),1);
	INIT_PZVAL(constval);
	ZVAL_LONG(constval,VXP_ERROR_BAD_CHAR);

	zend_hash_add(&ce->constants_table, VXP_XPATH_EXCEPTION_BAD_CHAR_NAME,
				  sizeof(VXP_XPATH_EXCEPTION_BAD_CHAR_NAME),
				  (void*)&constval,sizeof(zval*),NULL);


	constval=pemalloc(sizeof(*constval),1);
	INIT_PZVAL(constval);
	ZVAL_LONG(constval,VXP_ERROR);

	zend_hash_add(&ce->constants_table, VXP_XPATH_EXCEPTION_ERROR_NAME,
				  sizeof(VXP_XPATH_EXCEPTION_ERROR_NAME),
				  (void*)&constval,sizeof(zval*),NULL);

}

/* -------------- */

extern int vxp__scan_string(const char*, void* yyscanner);
extern int vxp_parse(void* yyscanner, struct Node** root, struct MemBlockInfo* mem);
extern int vxp_lex_init_extra(struct vxpState* state, void* yyscanner);
extern int vxp_lex_destroy(void* yyscanner);

/* {{{ vxp_functions[]
 *
 * Every user visible function must have an entry in vxp_functions[].
 */
const zend_function_entry vxp_functions[] = {
	PHP_FE(confirm_vxp_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(vxp_parse,	NULL)
	PHP_FE(vxp_unset,	NULL)
	PHP_FE(vxp_tree,	NULL)
	{NULL, NULL, NULL}	/* Must be the last line in vxp_functions[] */
};
/* }}} */


/* {{{ vxp_module_entry
 */
zend_module_entry vxp_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"vxp",
	vxp_functions,
	PHP_MINIT(vxp),
	PHP_MSHUTDOWN(vxp),
	PHP_RINIT(vxp),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(vxp),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(vxp),
#if ZEND_MODULE_API_NO >= 20010901
	"0.1", /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_VXP
ZEND_GET_MODULE(vxp)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("vxp.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_vxp_globals, vxp_globals)
    STD_PHP_INI_ENTRY("vxp.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_vxp_globals, vxp_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_vxp_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_vxp_init_globals(zend_vxp_globals *vxp_globals)
{
	vxp_globals->global_value = 0;
	vxp_globals->global_string = NULL;
}
*/
/* }}} */

static void vxp_resource_dtor(zend_rsrc_list_entry* rsrc TSRMLS_DC)
{
	struct vxpResource* vxpR=NULL;

	assert(rsrc);

	vxpR=(struct vxpResource*)rsrc->ptr;
	vxpFreeResource(vxpR);
}

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(vxp)
{
	zend_class_entry xpath_exception_ce;

	zend_register_long_constant("XPATH_10",sizeof("XPATH_10"),XPATH_10,CONST_CS|CONST_PERSISTENT,module_number TSRMLS_CC);
	zend_register_long_constant("XPATH_20",sizeof("XPATH_20"),XPATH_20,CONST_CS|CONST_PERSISTENT,module_number TSRMLS_CC);
	zend_register_long_constant("XPATH_21",sizeof("XPATH_21"),XPATH_21,CONST_CS|CONST_PERSISTENT,module_number TSRMLS_CC);
	zend_register_long_constant("VXP_ERROR",sizeof("VXP_ERROR"),VXP_ERROR,CONST_CS|CONST_PERSISTENT,module_number TSRMLS_CC);
	zend_register_long_constant("VXP_ERROR_SYNTAX",sizeof("VXP_ERROR_SYNTAX"),VXP_ERROR_SYNTAX,CONST_CS|CONST_PERSISTENT,module_number TSRMLS_CC);
	zend_register_long_constant("VXP_ERROR_MEMORY",sizeof("VXP_ERROR_MEMORY"),VXP_ERROR_MEMORY,CONST_CS|CONST_PERSISTENT,module_number TSRMLS_CC);
	/* If you have INI entries, uncomment these lines 
	REGISTER_INI_ENTRIES();
	*/
	le_vxp=zend_register_list_destructors_ex(vxp_resource_dtor,NULL,PHP_VXP_AST_RESOURCE_NAME, module_number);

	/* XPathException */
	INIT_CLASS_ENTRY(xpath_exception_ce, VXP_XPATH_EXCEPTION_NAME, vxp_xpath_exception_functions);
	vxp_xpath_exception_ce = zend_register_internal_class_ex(&xpath_exception_ce, 
																#if ZEND_MODULE_API_NO >= 20060613	
																	zend_exception_get_default(TSRMLS_C),
																#else
																	zend_exception_get_default(),
																#endif
			
																"Exception" TSRMLS_CC);
	vxp_xpath_exception_register_constants(vxp_xpath_exception_ce);

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(vxp)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(vxp)
{
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(vxp)
{
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(vxp)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "vxp support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */


/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_vxp_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_vxp_compiled)
{
	char *arg = NULL;
	int arg_len, len;
	char *strg;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
		return;
	}

	len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "vxp", arg);
	RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/

PHP_FUNCTION(vxp_parse)
{
	void* scanner=NULL;
	struct vxpState* state=NULL;
	struct Node* root=NULL;
	char* xpath=NULL;
	int xpath_length=0;
	long xpath_version=XPATH_10;
	struct MemBlockInfo* mem=NULL;
	struct vxpResource* res=NULL;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                         "s|l", &xpath,  &xpath_length, &xpath_version) == FAILURE) {
        RETURN_NULL();
    }

	mem=memInit(sizeof(*root));
	initState(&state,mem);
	vxp_lex_init_extra(state,&scanner);
	vxp__scan_string(xpath,scanner);
	vxp_parse(scanner, &root, mem);

	if (root)
	{
		res=vxpNewResource(root,mem);
		ZEND_REGISTER_RESOURCE(return_value, res, le_vxp);
	}
	else
		memFree(mem);

	efree(state);
	vxp_lex_destroy(scanner);
}

PHP_FUNCTION(vxp_unset)
{
	struct vxpResource* vxpR=NULL;

	zval* vxp_resource=NULL;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                         "r", &vxp_resource) == FAILURE) {
        RETURN_NULL();
    }

	ZEND_FETCH_RESOURCE(vxpR,struct vxpResource*, &vxp_resource,-1,
						PHP_VXP_AST_RESOURCE_NAME,le_vxp);

	Z_DELREF_P(vxp_resource);

	zend_hash_index_del(&EG(regular_list),Z_RESVAL_P(vxp_resource));

	RETURN_TRUE;
}

void vxpFreeResource(struct vxpResource* res)
{
	assert(res);
	assert(res->mem);

	memFree(res->mem);
	efree(res);
}

struct vxpResource* vxpNewResource(struct Node* node, struct MemBlockInfo* info)
{
	struct vxpResource* result=NULL;

	assert(node);
	assert(info);

	result=emalloc(sizeof(*result));

	if (!result)
	{
		memerror(sizeof(*result),__func__,__FILE__,__LINE__,info);
		return NULL;
	}

	result->root=node;
	result->mem=info;

	return result;
}

PHP_FUNCTION(vxp_tree)
{
	long xpath_version=XPATH_10;
	zval*  vxp_resource=NULL;
	struct vxpResource* resource;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                         "r|l", &vxp_resource, &xpath_version) == FAILURE) {
        RETURN_NULL();
    }

	ZEND_FETCH_RESOURCE(resource,struct vxpResource*, &vxp_resource,-1,
						PHP_VXP_AST_RESOURCE_NAME, le_vxp);

	assert(resource->mem);

	array_init(return_value);
	getXPath(resource->root, return_value, (int)xpath_version, resource->mem TSRMLS_CC);
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
